﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using DH.Web.Controllers;
using DH.Entity;
using DH.Service.Domain.User;
using Microsoft.AspNetCore.Authorization;
using DH.Web.Helper;
using DH.Web.Filter;
using DH.Service.ViewModels;
using Microsoft.AspNetCore.Localization;
using DH.Web.Attributes;
using DH.Service.Domain;

namespace DH.Web.MVC.Controllers
{
    [Route("Account/")]
    
    public class AccountController : SiteController<UserEntity, UserViewModel, string>
    {
        protected UserService _userService;
        protected PrivilegeService _privilegeService;
        
        public AccountController(IServiceCollection serviceCollection,
                                 UserService userService) : base(serviceCollection, userService)
        {
            _userService = userService;
            _privilegeService = serviceCollection.BuildServiceProvider().GetService<PrivilegeService>();
        }
        

        [AllowAnonymous]
        [HttpGet]
        [Route("Login")]
        public ActionResult Login(string returnUrl)
        {
            return View();
        }

        [HttpPost]
        [Route("Login")]
        public IActionResult Login(string userName,string userPass,string verifyCode)
        {
            userName = userName.Trim();
            userPass = userPass.Trim();
            ///todo:验证码 待完成

            UserEntity userModel;
            string msg = "登录成功";

            var checkRes = _userService.CheckUser(userName, userPass, out userModel, out msg);
            if (!checkRes)
            {
                //登录失败
                return Error(msg);
            }

            var session = new UserSession();
            session.UserId = userModel.DH_Account;
            session.UserName = userModel.DH_NickName;
            session.RealName = userModel.DH_RealName;
            session.IsAdmin = userModel.DH_IsAdministrator;
            session.DepartmentId = userModel.DH_DepartmentId;
            session.DutyId = userModel.DH_DutyId;
            session.RoleId = _privilegeService.GetUserRoles(userModel.Id);
            session.IsDeveloper = userModel.DH_IsDeveloper;
            session.UserImg = userModel.DH_HeadIcon;

            CurrentUser = session;

            var userSessionEx = new UserSessionEx(_service);
            userSessionEx.DepartmentId = CurrentUser.DepartmentId;
            userSessionEx.IsAdmin = CurrentUser.IsAdmin;
            userSessionEx.IsDeveloper = CurrentUser.IsDeveloper;
            userSessionEx.LoginIP = CurrentUser.LoginIP;
            userSessionEx.LoginTime = CurrentUser.LoginTime;
            userSessionEx.RealName = CurrentUser.RealName;
            userSessionEx.RoleId = CurrentUser.RoleId;
            userSessionEx.UserId = CurrentUser.UserId;
            userSessionEx.DepartmentId = CurrentUser.DepartmentId;
            userSessionEx.UserName = CurrentUser.UserName;
            userSessionEx.UserImg = CurrentUser.UserImg;

            CurrentUserEx = userSessionEx;

            return SuccessMessage(msg);
        }

        [Route("Logout")]
        [Authorize]
        public IActionResult Logout()
        {
            CurrentUser = null;
            return RedirectToAction("Login");
        }
        [UserPermissionFilter]
        public IActionResult VerifiyCode()
        {
            //HttpContext.SetSession("verifiyCode", "");

            return null;
        }
        [HttpPost]
        [Route("SetLanguage")]
        public IActionResult SetLanguage(string culture, string returnUrl)
        {
            Response.Cookies.Append(
                CookieRequestCultureProvider.DefaultCookieName,
                CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
                new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
            );

            return LocalRedirect(returnUrl);
        }


    }

   
}